package unei.net.nio;

import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.util.LinkedList;
import java.util.concurrent.*;

/**
 * Created by sunlei on 2015/8/31.
 */
public class SelectorManager {
    public static final SelectorManager INSTANCE=new SelectorManager();

    private Executor executor=createExecutor();

    private final LinkedList<SelectorWorker> freeWorkers=new LinkedList<SelectorWorker>();
    private int maxChannelsPerWorker=1024;

    private Executor createExecutor(){
        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(10, 1500, 30, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactory() {
            private int i=0;
            @Override
            public Thread newThread(Runnable r) {
                this.i++;
                final Thread t=new Thread(r,"NIO worker "+i);
                return t;
            }
        });

        return threadPoolExecutor;
    }

    public static SelectorManager getInstance(){
        return INSTANCE;
    }

    public synchronized SelectorSelection register(AbstractSelectableChannel channel,Listener listener) throws IOException {
        SelectorSelection selectorSelection=null;
        while(selectorSelection==null){
            if(freeWorkers.size()>0){
                SelectorWorker worker=freeWorkers.getFirst();
                if(worker.isReleased()){
                    freeWorkers.remove(worker);
                }else{
                    worker.retain();
                    selectorSelection=new SelectorSelection(worker,channel,listener);
                }
            }else{
                SelectorWorker worker=new SelectorWorker(this);
                freeWorkers.addFirst(worker);
                selectorSelection=new SelectorSelection(worker,channel,listener);
            }
        }
        return selectorSelection;
    }


    public interface Listener{
        void onSelect(SelectorSelection selectorSelection);
        void onError(SelectorSelection selectorSelection,Throwable throwable);
    }

    synchronized void onWorkerFullEvent(SelectorWorker worker) {
        freeWorkers.remove(worker);
    }

    public synchronized void onWorkerEmptyEvent(SelectorWorker worker) {
        freeWorkers.remove(worker);
    }

    public synchronized void onWorkerNotFullEvent(SelectorWorker worker) {
        freeWorkers.addFirst(worker);
    }

    public Executor getChannelExecutor() {
        return executor;
    }

    public void setChannelExecutor(Executor channelExecutor) {
        this.executor = channelExecutor;
    }

    public int getMaxChannelsPerWorker() {
        return maxChannelsPerWorker;
    }

    public void setMaxChannelsPerWorker(int maxChannelsPerWorker) {
        this.maxChannelsPerWorker = maxChannelsPerWorker;
    }

    public Executor getSelectorExecutor() {
        return executor;
    }

    public void setSelectorExecutor(Executor selectorExecutor) {
        this.executor = selectorExecutor;
    }
}
